﻿using FluentScheduler;
using PacoToolKit.Log;
using Protocol.OUSProtocol.Encoder;
using Protocol.OUSProtocol.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using TcpServer.OUServer;

namespace OUS_HUB.Jobs
{
    public class RealTimeJob : IJob
    {
        private FormMain Window;
        public RealTimeJob(FormMain window)
        {
            this.Window = window;
        }

        public void Execute()
        {
            try
            {
                foreach (var ous in Window.OUS_LIST.Where(x=>!x.IsUpdating))
                {
                    var info = new OUServerProtocolRequestInfo();
                    info.DAddrReverse = ous.DAddrReverse;
                    info.seqDec = 1;
                    info.C = "02";
                    info.AddrDec = 0;//从第0个字节开始读
                    info.DLenDec = Marshal.SizeOf(typeof(SStatus));//读取60个字节
                    info.Data = string.Empty;

                    string msg = BaseEncoder.CreateMsg(info.DAddrReverse, info.seqDec, info.C, info.AddrDec, info.DLenDec, info.Data);
                    Window.Write(ous.session, msg);
                }
            }
            catch (Exception ex)
            {
                Window.Log(ex.Message);
                Logger.Error(ex.Message, ex);
            }
        }
    }
}
